﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;

namespace StoragePeriod.COMP
{


    /// <summary>
    /// ADD BY PJ
    /// 2009-07-16
    /// </summary>
    public static class Data
    {
        /// <summary>
        /// 执行DataTable中的查询返回新的DataTable
        /// </summary>
        /// <param name="dt">源数据DataTable</param>
        /// <param name="condition">查询条件</param>
        /// <returns></returns>
        public static DataTable GetNewDataTable(DataTable dt, string condition)
        {
            DataTable newdt = new DataTable();
            newdt = dt.Clone();
            DataRow[] dr = dt.Select(condition);
            for (int i = 0; i < dr.Length; i++)
            {
                newdt.ImportRow((DataRow)dr[i]);
            }
            return newdt;//返回的查询结果
        }
        /// <summary>
        /// 判断DataSet是否为空，如果表中没有数据也返回false
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        /// <remarks>齐鹏飞 2011.04.12</remarks>
        public static bool DataSetIsNotNull(DataSet ds)
        {
            if (ds == null)
            {
                return false;
            }
            //是否存在表
            if (ds.Tables.Count == 0) { return false; }
            //表中是否有记录
            if (ds.Tables[0].Rows.Count == 0) { return false; }
            return true;
        }


        /// <summary>
        /// 根据值id获取值内容
        /// </summary>
        /// <param name="id">id值</param>
        /// <param name="valueName">字段为id的名称</param>
        /// <param name="txtName">返回的内容的字段名称</param>
        /// <param name="dt">数据表</param>
        /// <returns></returns>
        public static string GetTxtByValue(int id, string valueName, string txtName, DataTable dt)
        {
            try
            {
                //DataTable dt = mExpressAcc.GetTypeList();
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        if (row[valueName].ToString() == id.ToString())
                        {
                            return row[txtName].ToString();
                        }
                    }
                }
            }
            catch
            {
                return "";
            }
            return "";
        }

        /// <summary>
        /// 根据值id的一组值获取值内容
        /// </summary>
        /// <param name="id">id值</param>
        /// <param name="valueName">字段为id的名称</param>
        /// <param name="txtName">返回的内容的字段名称</param>
        /// <param name="dt">数据表</param>
        /// <returns></returns>
        public static string GetTxtByValue(string ids, string valueName, string txtName, DataTable dt)
        {
            //DataTable dt = mExpressAcc.GetTypeList();
            string strNames = "";
            string[] arr = ids.Split(',');
            try
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        for (int i = 0; i < arr.Length; i++)
                        {
                            if (row[valueName].ToString() == arr[i].ToString())
                            {
                                //return row[txtName].ToString();
                                strNames += row[txtName].ToString() + "，";
                            }
                        }
                    }
                }
                if (strNames.EndsWith("，"))
                {
                    strNames = strNames.Substring(0, strNames.Length - 1);
                }
            }
            catch
            {
                strNames = "";
            }
            return strNames;
        }

    }
}
